<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        //三种宠物的父类，定义共性
        var Pet = function (nickname, gender, owner, state = 80) {
            this.nickname = nickname
            this.gender = gender
            this.state = state
            this.owner = owner
            this.lastEatTime = new Date()
        }
        Pet.prototype.introduceSelf = function () {
            console.log(`
                大家好，我是一只叫${this.nickname}的${this.type},
                我是个${this.gender==="1"?"男":"女"}生,
                我的主人是${this.owner}，
                我的状态是${this.state}`)
        }
        Pet.prototype.eat = function () {
            //十秒间隔
            var now = new Date()
            if (now - this.lastEatTime < 10 * 1000) {
                console.log('我吃的太频繁啦，目前不需要吃东西哦！')
            } else {
                this.state += 20
                console.log(`嘻嘻，${this.nickname}吃饭了,当前状态${this.state}，今天也要冲呀`)
                this.lastEatTime = new Date()
            }
        }

        //三种宠物的play方法都不一致，所以单独定义
        var Dog = function () {
            this.type = "狗狗"
            Pet.apply(this, arguments)
        }
        Dog.prototype = new Pet()
        //方法建议写在原型上，因为可以减少new的时候重复创建function的内存开销
        Dog.prototype.play = function () {
            this.state -= 10
            console.log(`嘻嘻，${this.nickname}最喜欢玩飞盘啦,当前状态${this.state}`)
        }

        var Cat = function () {
            this.type = "猫咪"
            Pet.apply(this, arguments)
        }
        Cat.prototype = new Pet()
        Cat.prototype.play = function () {
            this.state -= 10
            console.log(`嘻嘻，${this.nickname}最喜欢玩毛线球啦,当前状态${this.state}`)
        }

        var Fish = function () {
            this.type = "鱼"
            Pet.apply(this, arguments)
        }
        Fish.prototype = new Pet()
        Fish.prototype.play = function () {
            this.state -= 10
            console.log(`嘻嘻，${this.nickname}最喜欢仰泳啦,当前状态${this.state}`)
        }

        //用户类
        var User = function (nickname, password) {
            this.nickname = nickname
            this.password = password
        }

        var game = {
            loginUser: null,
            //模拟用户数据库
            userList: [],
            //模拟宠物数据库
            petList: [],
            //敏感词数组
            sensitiveWords: ["日本", "土共", "赤匪","共匪"],
            //检测字符串中是否包含敏感词
            checkSensitiveWord(str) {
                return this.sensitiveWords.some(item => {
                    return str.includes(item)
                })
            },
            //欢迎界面
            showWelcomePage() {
                var result = prompt("欢迎来到宠物游戏，请选择操作： 1.登录   2.注册   3.退出")
                switch (result) {
                    case "1":
                        this.showLoginPage()
                        break;
                    case "2":
                        this.showRegisterPage()
                        break;
                    case "3":
                        console.log("欢迎下次使用！")
                        break;
                    default:
                        console.log("输入有误！")
                        arguments.callee.call(this)
                        break;
                }
            },
            //注册界面
            showRegisterPage() {
                //输入昵称
                do {
                    var isOk = true
                    var nickname = prompt("注册：请输入昵称  ：")
                    if (!nickname || !nickname.trim()) {
                        isOk = false
                        console.log("输入的昵称不能为空！")
                    } else {
                        var isSensitived = this.checkSensitiveWord(nickname)
                        if (isSensitived) {
                            isOk = false
                            console.log("昵称包含非法字符，请重新输入")
                        } else {
                            //检测用户是否存在
                            var isAlive = this.checkNicknameRepeated(nickname)
                            if (isAlive) {
                                isOk = false
                                console.log("用户名已经存在，注册失败")
                            }
                        }
                    }
                } while (!isOk);
                //输入密码
                do {
                    var password = prompt("注册-请输入密码  ：")
                    var isOkPwd = password && password.trim().length >= 6 && password.trim().length <= 20
                    if (!isOkPwd) {
                        console.log("密码输入有误，请输入长度为6-20之间的密码")
                    }
                } while (!isOkPwd);
                //确认密码
                do {
                    var rePassword = prompt("注册-请确认密码  ：")
                    var isOkRePwd = rePassword === password
                    if (!isOkRePwd) {
                        console.log("两次输入密码不一致！")
                        isOkRePwd = false
                    }
                } while (!isOkRePwd);
                this.doRegister(nickname.trim(), password.trim())
                this.showWelcomePage()
            },
            //验证注册的用户名是否重复
            checkNicknameRepeated(nickname) {
                return this.userList.some(function (item) {
                    return item.nickname === nickname
                })
            },
            //注册
            doRegister(nickname, password) {
                this.userList.push(new User(nickname.trim(), password.trim()))//{nickName:'',password:''}

            },
            //登录界面
            showLoginPage() {
                //验证昵称
                do {
                    var nickname = prompt("请输入昵称：")
                    if (!nickname || !nickname.trim()) {
                        console.log("昵称格式不正确！")
                    } else {
                        var userAlive = this.checkNicknameIsAlive(nickname)
                        if (!userAlive) {
                            console.log("用户名不存在")
                        }
                    }

                } while (!nickname || !userAlive);
                //验证密码
                do {
                    var password = prompt("请输入密码：")
                    if (!password || !password.trim()) {
                        console.log("密码格式不正确！")
                    }
                } while (!password);
                //验证用户名和密码是否在数组中
                var isAlive = this.doLogin(nickname, password)
                if (isAlive) {
                    console.log("欢迎用户：" + nickname + "登录成功")
                    this.loginUser = nickname
                    this.showMainPage()
                } else {
                    console.log("昵称或者密码错误，请重新输入")
                    arguments.callee.call(this)
                }
            },
            //登录验证用户名和密码是否正确
            doLogin(nickname, password) {
                return this.userList.some(function (item) {
                    return item.nickname === nickname.trim() && item.password === password.trim()
                })
            },
            //检查用户名是否已经存在
            checkNicknameIsAlive(nickname) {
                return this.userList.some(function (item) {
                    return item.nickname === nickname.trim()
                })
            },
            //登录成功后跳转到主界面
            showMainPage() {
                var result = prompt("欢迎来到宠物游戏，请操作：1、购买宠物 2、查看我的宠物 3、注销")
                switch (result) {
                    case "1":
                        this.showBuyPetPage()
                        break;
                    case "2":
                        this.showMyPetPage()
                        break;
                    case "3":
                        this.showWelcomePage()
                        break;
                    default:
                        console.log("输入有误 ")
                        arguments.callee.call(this)
                        break;
                }
            },
            //购买宠物
            showBuyPetPage() {
                var myPet = this.getMyPet()
                if (myPet) {
                    console.log("您已经有宠物了，请好好对" + (myPet.gender === "1" ? "他" : "她"));
                    this.showMainPage()
                    return false;
                }

                //选择宠物类型
                do {
                    var isOk = false
                    var type = prompt("请选择宠物类型：1:狗狗 2:猫咪 3:鱼")
                    if (["1", "2", "3"].includes(type)) {
                        isOk = true
                    } else {
                        console.log("类型有误，请重新输入！")
                    }
                } while (!isOk)

                //输入宠物昵称
                do {
                    var nickname = prompt("请输入宠物的昵称：")
                    if (!nickname || !nickname.trim()) {
                        isOk = false
                        console.log("昵称格式不正确！")
                    } else {
                        var isSensitived = this.checkSensitiveWord(nickname)
                        if (isSensitived) {
                            isOk = false
                            console.log("宠物昵称包含非法字符，请重新输入")
                        }
                    }
                } while (!nickname)
                //输入宠物性别
                do {
                    var isOk = false
                    var gender = prompt("请输入宠物的性别：1.GG  2.MM")
                    if (["1", "2"].includes(gender)) {
                        isOk = true
                    } else {
                        console.log("性别输入有误，请重新输入！")
                    }
                } while (!isOk)
                //将宠物信息传给 doMyPet
                this.doBuyPet(nickname, gender, this.loginUser, type)
                console.log("领养成功 ~");
                this.showMainPage()
            },
            //做一次购买宠物
            doBuyPet(nickname, gender, owner, type) {
                var pet = null
                switch (type) {
                    case "1":
                        pet = new Dog(nickname, gender, this.loginUser)
                        break;
                    case "2":
                        pet = new Cat(nickname, gender, this.loginUser)
                        break;
                    case "3":
                        pet = new Fish(nickname, gender, this.loginUser)
                        break;
                }
                this.petList.push(pet)
            },
            //获取我的宠物
            getMyPet() {
                return this.petList.find(item => {
                    return item.owner === this.loginUser
                })
            },
            //查看我的宠物
            showMyPetPage() {
                var mypet = this.getMyPet()
                if (!mypet) {
                    console.log("您还没有宠物，快去领养一只把 ~");
                    this.showMainPage()
                    return
                }
                
                console.log(mypet.introduceSelf())

                var result = prompt("请操作：1、喂食 2、玩耍 3、回到主菜单")
                switch (result) {
                    case "1":
                        this.doFeedPet(mypet)
                        break;
                    case "2":
                        this.doPlayWithPet(mypet)
                        break;
                    case "3":
                        return this.showMainPage()
                    default:
                        console.log("输入有误 ")
                        break;
                }

                arguments.callee.call(this)
            },
            //喂养宠物
            doFeedPet(pet) {
                pet.eat()
            },
            //从我的宠物中跳转到宠物操作页面
            doPlayWithPet(pet) {
                pet.play()
            },
            //开始游戏
            start() {
                //测试初始化账户
                this.doRegister("ace", "123123")
                //显示欢迎页面
                this.showWelcomePage()
            }
        }

        game.start()
    </script>
</body>

</html>
